package com.example.websocket.excel.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 操作时间方法
 * 
 * @author Gaojt
 *
 */
public class DateUtils {
	private static final DateFormat yearForamt = new SimpleDateFormat("yyyy");
	private static final DateFormat monthForamt = new SimpleDateFormat("yyyy-MM");
	private static final DateFormat dayForamt = new SimpleDateFormat("yyyy-MM-dd");
	private static final DateFormat minuteForamt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final DateFormat secondForamt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final DateFormat gbkForamt = new SimpleDateFormat("yyyy年M月d日");
	private static final DateFormat gbkLongForamt = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	// 常见日期格式
	private static final String[][] normalFormat = new String[][] {
			{ "\\d{4}-[0-1]?\\d{1}-[0-3]?\\d{1}", "yyyy-MM-dd" },
			{ "\\d{4}-[0-1]?\\d{1}-[0-3]?\\d{1} [0-2]?\\d{1}:[0-5]?\\d{1}:[0-5]?\\d{1}", "yyyy-MM-dd HH:mm:ss" },
			{ "\\d{4}-[0-1]?\\d{1}-[0-3]?\\d{1} [0-2]?\\d{1}:[0-5]?\\d{1}", "yyyy-MM-dd HH:mm" },
			{ "\\d{4}\\.[0-1]?\\d{1}\\.[0-3]?\\d{1}", "yyyy.MM.dd" },
			{ "\\d{4}\\.[0-1]?\\d{1}\\.[0-3]?\\d{1} [0-2]?\\d{1}:[0-5]?\\d{1}:[0-5]?\\d{1}", "yyyy.MM.dd HH:mm:ss" },
			{ "\\d{4}\\.[0-1]?\\d{1}\\.[0-3]?\\d{1} [0-2]?\\d{1}:[0-5]?\\d{1}", "yyyy.MM.dd HH:mm" },
			{ "\\d{4}/[0-1]?\\d{1}/[0-3]?\\d{1}", "yyyy/MM/dd" },
			{ "\\d{4}/[0-1]?\\d{1}/[0-3]?\\d{1} [0-2]?\\d{1}:[0-5]?\\d{1}:[0-5]?\\d{1}", "yyyy/MM/dd HH:mm:ss" },
			{ "\\d{4}/[0-1]?\\d{1}/[0-3]?\\d{1} [0-2]?\\d{1}:[0-5]?\\d{1}", "yyyy/MM/dd HH:mm" },
			{ "\\d{4}年[0-1]?\\d{1}月[0-3]?\\d{1}日", "yyyy年MM月dd日" },
			{ "\\d{4}年[0-1]?\\d{1}月[0-3]?\\d{1}日 [0-2]?\\d{1}小时[0-5]?\\d{1}分[0-5]?\\d{1}秒", "yyyy年MM月dd日HH小时mm分ss秒" },
			{ "\\d{4}年[0-1]?\\d{1}月[0-3]?\\d{1}日 [0-2]?\\d{1}小时[0-5]?\\d{1}分", "yyyy年MM月dd日HH小时mm分" },
			{ "\\d{4}-[0-1]?\\d{1}", "yyyy-MM" } };

	/**
	 * 取得日期对应位的最大值
	 * 
	 * @param date  日期对象
	 * @param field Calendar.YEAR/Calendar.MONTH等
	 * @return
	 */
	public static int getActualMinimum(Date date, int field) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getActualMinimum(field);
	}

	/**
	 * 取得日期对应位的最大值
	 * 
	 * @param date  日期对象
	 * @param field Calendar.YEAR/Calendar.MONTH等
	 * @return
	 */
	public static int getActualMaximum(Date date, int field) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getActualMaximum(field);
	}

	/**
	 * 取得日期对应位的值
	 * 
	 * @param date  日期对象
	 * @param field Calendar.YEAR/Calendar.MONTH等
	 * @return
	 */
	public static int get(Date date, int field) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(field);
	}

	/**
	 * 设置日期对应位的值
	 * 
	 * @param date  日期对象
	 * @param field Calendar.YEAR/Calendar.MONTH等
	 * @param value 值
	 * @return
	 */
	public static Date set(Date date, int field, int value) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(field, value);
		return c.getTime();
	}

	/**
	 * 增减日期对应位的值
	 * 
	 * @param date  日期对象
	 * @param field Calendar.YEAR/Calendar.MONTH等
	 * @param value 值
	 * @return
	 */
	public static Date add(Date date, int field, int value) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(field, value);
		return c.getTime();
	}

	/**
	 * 验证时间是否在今天之前
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isBeforeNow(Date date) {
		Calendar now = Calendar.getInstance();
		Calendar when = Calendar.getInstance();
		when.setTime(date);
		return now.before(when);
	}

	/**
	 * 验证两个时间对象的前后
	 * 
	 * @param before
	 * @param after
	 * @return before < after：-1；before = after：0；before > after：1
	 */
	public static int compareIndex(Date before, Date after) {
		Calendar b = Calendar.getInstance();
		b.setTime(before);
		Calendar a = Calendar.getInstance();
		a.setTime(after);
		return b.compareTo(a);
	}

	/**
	 * 验证两个时间相差小时数
	 * 
	 * @param before
	 * @param after
	 * @return before < after：负数；before = after：0；before > after：正数
	 */
	public static int compareHour(Date before, Date after) {
		Calendar b = Calendar.getInstance();
		b.setTime(before);
		Calendar a = Calendar.getInstance();
		a.setTime(after);
		long min = b.getTimeInMillis() - a.getTimeInMillis();
		long hour = 1000 * 60 * 60;
		double rs = min / hour;
		return (int) rs;
	}

	/**
	 * 验证两个时间相差日期
	 * 
	 * @param before
	 * @param after
	 * @return before < after：负数；before = after：0；before > after：正数
	 */
	public static int compareDate(Date before, Date after) {
		Calendar b = Calendar.getInstance();
		b.setTime(before);
		Calendar a = Calendar.getInstance();
		a.setTime(after);
		long min = b.getTimeInMillis() - a.getTimeInMillis();
		long date = 1000 * 60 * 60 * 24;
		double rs = min / date;
		return (int) rs;
	}

	/**
	 * 验证两个时间相差日期
	 * 
	 * @param before
	 * @param after
	 * @return before < after：负数；before = after：0；before > after：正数
	 */
	public static int compareMonth(Date before, Date after) {
		Calendar b = Calendar.getInstance();
		b.setTime(before);
		Calendar a = Calendar.getInstance();
		a.setTime(after);
		int result = a.get(Calendar.MONTH) - b.get(Calendar.MONTH);
		int month = (a.get(Calendar.YEAR) - b.get(Calendar.YEAR)) * 12;
		return month + result;
	}

	/**
	 * 比较两个时间大小
	 * 
	 * @param before
	 * @param after
	 * @return before>after前等于1，before<after后等于-1，before等于after等于0
	 */
	public static int compareMonth(String before, String after) {
		try {
			Date dt1 = dayForamt.parse(before);
			Date dt2 = dayForamt.parse(after);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 根据日期获取该日期是该年的多少天
	 * 
	 * @param date
	 * @return
	 */
	public static int getDayNumForYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 获取一个月天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String toString(Date date) {
		return toString(date, dayForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String toSecondString(Date date) {
		return toString(date, secondForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm
	 * 
	 * @param date
	 * @return
	 */
	public static String toMinuteString(Date date) {
		return toString(date, minuteForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String toString(Date date, String reg) {
		DateFormat format;
		if (reg == null)
			format = dayForamt;
		else
			format = new SimpleDateFormat(reg);
		return toString(date, format);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String toString(Date date, DateFormat format) {
		if (date == null || format == null)
			return null;
		return format.format(date);
	}

	/**
	 * 将日期转换成标准格式yyyy年MM月dd日
	 * 
	 * @param date
	 * @return
	 */
	public static String toLocaleString(Date date) {
		return toString(date, gbkForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy年MM月dd日 HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String toLocaleLongString(Date date) {
		return toString(date, gbkLongForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy年MM月
	 * 
	 * @param date
	 * @return
	 */
	public static String toLocaleShotString(Date date) {
		return toString(date, monthForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static Date parse(String str) {
		if (str == null || str.trim().length() == 0)
			return null;
		for (int i = 0; i < normalFormat.length; i++) {
			String reg = normalFormat[i][0];
			String pat = normalFormat[i][1];
			if (Pattern.matches(reg, str)) {
				try {
					return new SimpleDateFormat(pat).parse(str);
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
			}
		}
		throw new RuntimeException(str + "不是常规日期格式，无法自动转换，请指定转换格式！");
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static Date parseSecond(String str) {
		return parse(str, secondForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm
	 * 
	 * @param date
	 * @return
	 */
	public static Date parseMinute(String str) {
		return parse(str, minuteForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static Date parse(String str, String reg) {
		DateFormat format;
		if (reg == null)
			format = dayForamt;
		else
			format = new SimpleDateFormat(reg);
		return parse(str, format);
	}

	/**
	 * 将日期转换成标准格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static Date parse(String str, DateFormat format) {
		if (str == null || str.trim().length() == 0)
			return null;
		if (format == null)
			format = dayForamt;
		try {
			return format.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 将日期转换成标准格式yyyy年MM月dd日
	 * 
	 * @param date
	 * @return
	 */
	public static Date parseLocale(String str) {
		return parse(str, gbkForamt);
	}

	/**
	 * 将日期转换成标准格式yyyy年MM月dd日 HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static Date parseLocaleLong(String str) {
		return parse(str, gbkLongForamt);
	}

	/**
	 * 检测字符串是否是类似2003-12-02 23:12:39的格式
	 * 
	 * @param ds
	 * @return
	 */
	public static boolean isDate(String ds) {
		if (ds == null)
			return false;
		ds = ds.trim();
		Pattern p = Pattern.compile("^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$");
		return p.matcher(ds).matches();
	}

	/**
	 * 将日期时间设置为一天开始时间
	 * 
	 * @param date 日期
	 * @return 转换后的日期
	 */
	public static Date minDay(Date date) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
		c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
		return c.getTime();
	}

	/**
	 * 将日期时间设置为一周开始时间
	 * 
	 * @param date 日期
	 * @return 转换后的日期
	 */
	public static Date minWeek(Date date) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getActualMinimum(Calendar.DAY_OF_WEEK));
		c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
		c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
		return c.getTime();
	}

	/**
	 * 将日期时间设置为一月开始时间
	 * 
	 * @param date 日期
	 * @return 转换后的日期
	 */
	public static Date minMonth(Date date) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
		c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
		return c.getTime();
	}

	/**
	 * 将日期时间设置为一年开始时间
	 * 
	 * @param date 日期
	 * @return 转换后的日期
	 */
	public static Date minYear(Date date) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.MONTH, c.getActualMinimum(Calendar.MONTH));
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
		c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
		return c.getTime();
	}

	/**
	 * 将日期时间设置为一天结束时间
	 * 
	 * @param date          日期
	 * @param isMilliSecond 是否设置毫秒，用于兼容MySQL无法存储毫秒数据问题
	 * @return 转换后的日期
	 */
	public static Date maxDay(Date date, boolean... isMilliSecond) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, c.getActualMaximum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMaximum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMaximum(Calendar.SECOND));
		if (isMilliSecond == null || isMilliSecond.length == 0 || isMilliSecond[0])
			c.set(Calendar.MILLISECOND, c.getActualMaximum(Calendar.MILLISECOND));
		return c.getTime();
	}

	/**
	 * 将日期时间设置为一周开始时间
	 * 
	 * @param date          日期
	 * @param isMilliSecond 是否设置毫秒，用于兼容MySQL无法存储毫秒数据问题
	 * @return 转换后的日期
	 */
	public static Date maxWeek(Date date, boolean... isMilliSecond) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getActualMaximum(Calendar.DAY_OF_WEEK));
		c.set(Calendar.HOUR_OF_DAY, c.getActualMaximum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMaximum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMaximum(Calendar.SECOND));
		if (isMilliSecond == null || isMilliSecond.length == 0 || isMilliSecond[0])
			c.set(Calendar.MILLISECOND, c.getActualMaximum(Calendar.MILLISECOND));
		return c.getTime();
	}

	/**
	 * 将日期时间设置为一月开始时间
	 * 
	 * @param date          日期
	 * @param isMilliSecond 是否设置毫秒，用于兼容MySQL无法存储毫秒数据问题
	 * @return 转换后的日期
	 */
	public static Date maxMonth(Date date, boolean... isMilliSecond) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, c.getActualMaximum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMaximum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMaximum(Calendar.SECOND));
		if (isMilliSecond == null || isMilliSecond.length == 0 || isMilliSecond[0])
			c.set(Calendar.MILLISECOND, c.getActualMaximum(Calendar.MILLISECOND));
		return c.getTime();
	}

	/**
	 * 将日期时间设置为一年开始时间
	 * 
	 * @param date          日期
	 * @param isMilliSecond 是否设置毫秒，用于兼容MySQL无法存储毫秒数据问题
	 * @return 转换后的日期
	 */
	public static Date maxYear(Date date, boolean... isMilliSecond) {
		if (date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.MONTH, c.getActualMaximum(Calendar.MONTH));
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, c.getActualMaximum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getActualMaximum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getActualMaximum(Calendar.SECOND));
		if (isMilliSecond == null || isMilliSecond.length == 0 || isMilliSecond[0])
			c.set(Calendar.MILLISECOND, c.getActualMaximum(Calendar.MILLISECOND));
		return c.getTime();
	}
	

	/**
	 * 验证两个日期的年、月、日等是否相等，判断由年、月、日逐级至field判断，所有属性相等才相等。例如：2018年12月不等于2017年12月
	 * 
	 * @param data1 比较日期1
	 * @param date2 比较日期2
	 * @param field Calendar.YEAR/Calendar.MONTH等
	 * @return
	 */
	public static int compareTo(Date data1, Date date2, int field) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(data1);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(date2);
		int rs = c1.compareTo(c2);
		if (rs != 0) {
			List<Integer> fields = Arrays.asList(Calendar.MILLISECOND, Calendar.SECOND, Calendar.MINUTE,
					Calendar.DAY_OF_YEAR, Calendar.MONTH, Calendar.YEAR);
			int start = fields.indexOf(field);
			if (start == -1)
				throw new RuntimeException(
						"field入参仅支持Calendar.MILLISECOND,\r\n" + "Calendar.SECOND,\r\n" + "Calendar.MINUTE,\r\n"
								+ "Calendar.DAY_OF_YEAR,\r\n" + "Calendar.MONTH,\r\n" + "Calendar.YEAR");
			Iterator<Integer> iter = fields.listIterator(start);
			while (iter.hasNext()) {// 遍历，校验各个属性域的值是否相等
				int ifield = iter.next();
				if (c1.get(ifield) != c2.get(ifield)) {
					return rs;
				}
			}
			return 0;
		}
		return rs;
	}
	
	/**
	 * 获取当前日期的下一个月
	 * @return
	 */
	public static Date getNextMonth() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 1);
		return cal.getTime();
	}

	/**
	 * 判断两个日期是闰年还是平年
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static boolean isOneYear(Date start, Date end) {
		Calendar startday = Calendar.getInstance();
		Calendar endday = Calendar.getInstance();
		startday.setTime(start);
		endday.setTime(end);
		endday.add(Calendar.DATE, 1);
		if (startday.after(endday)) {
			return false;
		}
		long sl = startday.getTimeInMillis();
		long el = endday.getTimeInMillis();
		long days = ((el - sl) / (1000 * 3600 * 24));
		if (days == 365 || days == 366) {
			if (startday.get(Calendar.MONTH) <= 1) {
				startday.set(Calendar.MONTH, 1);
				int lastDay = startday.getActualMaximum(Calendar.DAY_OF_MONTH);
				return (lastDay == 28 && days == 365) || (lastDay == 29 && days == 366);
			} else {
				endday.set(Calendar.MONTH, 1);
				int lastDay = endday.getActualMaximum(Calendar.DAY_OF_MONTH);
				return (lastDay == 28 && days == 365) || (lastDay == 29 && days == 366);
			}
		} else {
			return false;
		}
	}

	/**
	 * 计算两个日期相差月数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getMonthSpace(String date1, String date2) {
		int result = 0;
		if (compareMonth(date1, date2) == -1) { // 如果date1 < date2
			try {
				Calendar c1 = Calendar.getInstance();
				Calendar c2 = Calendar.getInstance();
				c1.setTime(dayForamt.parse(date1));
				c2.setTime(dayForamt.parse(date2));
				c2.add(Calendar.DATE, 1);
				int month = c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
				int year = (c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR)) * 12;
				result = year + month;
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return result == 0 ? 1 : Math.abs(result);
		}
		return result;
	}

	/**
	 * 得到一个月有多少天
	 * @param date
	 * @return
	 */
	public static Integer getDaySpace(Date date) {
		Integer betweenDay = null;
		try {
			Date currentTime = dayForamt.parse(dayForamt.format(new Date()));
			Date pastTime = dayForamt.parse(dayForamt.format(date));
			Calendar cal = Calendar.getInstance();
			cal.setTime(currentTime);
			long time1 = cal.getTimeInMillis();
			cal.setTime(pastTime);
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / (1000 * 3600 * 24);
			betweenDay = Integer.parseInt(String.valueOf(between_days));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return betweenDay;
	}

	/**
	 * 获取前/后半年的开始时间
	 * 
	 * @return
	 */
	public static Date getHalfYearStartTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 11);
			} else if (currentMonth >= 7 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 5);
			}
			c.set(Calendar.DATE, 1);
			now = secondForamt.parse(dayForamt.format(c.getTime()) + " 00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 根据时间得到上半年提醒收款时间
	 * 
	 * @param date
	 * @return 上半年
	 */
	public static Date getFirstPhaseDate(String date) {
		Date now = null;
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(dayForamt.parse(date));
			int month = cal.get(Calendar.MONTH);
			int day = cal.get(Calendar.DATE);
			if (day == 1) {
				cal.set(Calendar.MONTH, month);
			} else if (day > 1) {
				cal.set(Calendar.MONTH, month + 1);
			}
			cal.set(Calendar.DATE, 1);
			now = dayForamt.parse(dayForamt.format(cal.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return now;
	}

	public static Date getHalfYearDate(String date) {
		Date now = null;
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(dayForamt.parse(date));
			int month = cal.get(Calendar.MONTH);
			cal.set(Calendar.MONTH, (month + 6));
			cal.set(Calendar.DATE, 1);
			now = dayForamt.parse(dayForamt.format(cal.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 根据时间获取每隔3月提醒收款时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getQuarterTime(String date) {
		Date now = null;
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(dayForamt.parse(date));
			int month = cal.get(Calendar.MONTH);
			cal.set(Calendar.MONTH, (month + 3));
			cal.set(Calendar.DATE, 1);
			now = dayForamt.parse(dayForamt.format(cal.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获取月份付款日期
	 * 
	 * @param date
	 * @return
	 */
	public static String[] getTheMonths(String date, int size) {
		String[] last12Months = new String[size];
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(dayForamt.parse(date));
			int day = cal.get(Calendar.DATE);
			if (day == 1) {
				cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
			}
			cal.set(Calendar.DAY_OF_MONTH, 1);
			last12Months[0] = dayForamt.format(cal.getTime());
			for (int i = 0; i < size; i++) {
				cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); // 逐次往前推1个月
				last12Months[i] = dayForamt.format(cal.getTime());
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return last12Months;
	}

	/**
	 * 比较两个时间相差多少个小时
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Long getTimeSub(Date date) throws Exception {
		String dateString = secondForamt.format(date);
		String currentDate = secondForamt.format(new Date());
		Long sub = secondForamt.parse(dateString).getTime() - secondForamt.parse(currentDate).getTime();
		return sub;
	}

	/**
	 * 判断传入的日期是否是昨天
	 * 
	 * @param date
	 * @return
	 */
	public static Boolean isYesterday(Date date) {
		Boolean flag = false;
		// 先获取年份
		int year = Integer.valueOf(yearForamt.format(date));
		// 获取当前年分 和 一年中的第几天
		int day = getDayNumForYear(date);
		int currentYear = Integer.valueOf(yearForamt.format(new Date()));
		Integer currentDay = getDayNumForYear(new Date());
		// 计算 如果是去年
		if (currentYear - year == 1) {
			// 如果当前正好是 1月1日 计算去年有多少天，指定时间是否是一年中的最后一天
			if (currentDay == 1) {
				int yearDay;
				if (year % 400 == 0) {
					// 世纪闰年
					yearDay = 366;
				} else if (year % 4 == 0 && year % 100 != 0) {
					// 普通闰年
					yearDay = 366;
				} else {
					// 平年
					yearDay = 365;
				}
				if (day == yearDay) {
					flag = true;
				}
			}
		} else {
			if (currentDay - day == 1) {
				flag = true;
			}
		}
		return flag;
	}

	/**
	 * 获取开票截止日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getInvoiceExpiryDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		cal.set(Calendar.DAY_OF_MONTH, 15);
		return cal.getTime();
	}
	

	/**
	 * 获取月开始时间和结束时间
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getMonthTime(int monthSet) throws Exception {
		Long startTime = getMonthStartTime(monthSet);
		Long endTime = getMonthEndTime(monthSet);
		DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String startTimeStr = ftf
				.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault()));
		String endTimeStr = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault()));
		Map<String, Object> map = new HashMap<>();
		map.put("startDate", startTimeStr);
		map.put("endDate", endTimeStr);
		return map;
	}

	/**
	 * 获取月的开始时间
	 * @param monthSet
	 * @return
	 * @throws Exception
	 */
	public static Long getMonthStartTime(int monthSet) throws Exception {
		Long currentTime = System.currentTimeMillis();
		String timeZone = "GMT+8:00";
		Calendar calendar = Calendar.getInstance(); // 获取当前日期
		calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
		calendar.setTimeInMillis(currentTime);
		calendar.add(Calendar.YEAR, 0);
		calendar.add(Calendar.MONTH, monthSet);
		calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTimeInMillis();
	}
	
	/**
	 * 获取指定年月的开始时间
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getBeginTime(int year, int month) {
		YearMonth yearMonth = YearMonth.of(year, month);
		LocalDate localDate = yearMonth.atDay(1);
		LocalDateTime startOfDay = localDate.atStartOfDay();
		ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
		return Date.from(zonedDateTime.toInstant());
	}

	/**
	 * 获取月的结束时间
	 * @param monthSet
	 * @return
	 */
	public static Long getMonthEndTime(int monthSet) {
		Long currentTime = System.currentTimeMillis();
		String timeZone = "GMT+8:00";
		Calendar calendar = Calendar.getInstance();// 获取当前日期
		calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
		calendar.setTimeInMillis(currentTime);
		calendar.add(Calendar.YEAR, 0);
		calendar.add(Calendar.MONTH, monthSet);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTimeInMillis();
	}
	
	/**
	 * 获取指定年月结束时间
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getEndTime(int year, int month) {
		YearMonth yearMonth = YearMonth.of(year, month);
		LocalDate endOfMonth = yearMonth.atEndOfMonth();
		LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
		ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
		return Date.from(zonedDateTime.toInstant());
	}


}
